package com.xry.user.service;

import com.xry.common.bean.User;
import com.xry.common.service.UserService;
import com.xry.common.service.LoginService;
import com.xry.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserService userService;

    @Override
    public User rest(String oldpassword, String newpassword, Integer id) {
        if (id==null){
            throw new RuntimeException("用户未登录");
        }
        User user = userService.getUserById(id);
        if(!PasswordUtil.checkPassword(oldpassword,user.getPassword())){
            throw new RuntimeException("旧密码错误");
        }
        user.setPassword(PasswordUtil.getEncryption(newpassword));
        userService.updateUserById(user);
        return user;
    }

    @Override
    public User logininfo(String token) {
        //token中存的用户id
        Object id=TokenUtil.getOption(token, SYS.TOKEN_ID);
        if (id==null){
            throw new RuntimeException("登录异常");
        }
        User user = userService.getUserById(Integer.parseInt(id.toString()));
        return user;
    }

    @Override
    public String login(User user) {
        //登录逻辑
        //1.通过用户名查询数据库
        User result = userService.getUserByUserName(user.getUsername());
        if(result==null){
            throw new RuntimeException("登录异常");
        }
        //2.验证密码
        String password= user.getPassword();
        String encryptionPassword = result.getPassword();
        if(!PasswordUtil.checkPassword(password,encryptionPassword)){
            throw new RuntimeException("密码错误");
        }
        //3.返回结果token
        String token = TokenUtil.getToken(result.getId(),result.getRoleid());
        System.out.println(result.getRoleid());
        return token;
    }


    @Override
    public void sendResetCode(String email) {
        // 验证邮箱是否存在
        User user = userService.getByEmail(email);
        if (user == null) {
            throw new RuntimeException("该邮箱未注册");
        }
        // 生成验证码
        String code = VerificationCodeUtil.generateCode();
        // 发送邮件
        try {
            EmailUtil.sendEmail(email, code);
            // 存储验证码
            VerificationCodeUtil.storeCode(email, code);
        } catch (Exception e) {
            throw new RuntimeException("发送验证码失败，请稍后重试");
        }
    }

    @Override
    public void resetByEmail(String email, String code, String newPassword) {
        // 验证验证码
        if (!VerificationCodeUtil.verifyCode(email, code)) {
            throw new RuntimeException("验证码错误或已过期");
        }
        // 验证用户存在
        User user = userService.getByEmail(email);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        // 更新密码
        user.setPassword(PasswordUtil.getEncryption(newPassword));
        userService.updateUserById(user);
        // 移除验证码
        VerificationCodeUtil.removeCode(email);
    }

}
